﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace SlaktDataReader.Pages
{
    /// <summary>
    /// Interaction logic for pgBirth.xaml
    /// </summary>
    public partial class pgBirth : Page, INotifyPropertyChanged
    {

        private Book b;
        private BirthBook bd;
        private BirthBookRecord br;

        //private ObservableCollection<BirthBookRecord> bbR;
        CollectionView view;

        public Grid parentgrid;

        public BirthBook BirthBookData
        {
            get { return bd; }
            set
            {
                if (bd != value)
                {
                    bd = value;
                    Notify("BirthBookData");
                }
            }
        }
        public Book BirthBook
        {
            get { return b; }
            set
            {
                if (b != value)
                {
                    b = value;
                    Notify("BirthBook");
                }
            }
        }

        public BirthBookRecord CurItem
        {
            get { return br; }
            set { if (br != value) { br = value; Notify("CurItem"); } }
        }

        public pgBirth()
        {
            InitializeComponent();

            bd = new SlaktDataReader.BirthBook();

            bd.ExportFail += new EventHandler(BirhtBookData_ExportFail);
            bd.ExportSuccess += new EventHandler(BirhtBookData_ExportSuccess);
            bd.ReadFail += new EventHandler(BirhtBookData_ReadFail);
            bd.ReadSuccess += new EventHandler(BirhtBookData_ReadSuccess);


        }

        void BirhtBookData_ReadSuccess(object sender, EventArgs e)
        {
            Notify("BirthBookData");
            view = (CollectionView)CollectionViewSource.GetDefaultView(lvBirth.ItemsSource);

            view.Filter = _filter;

        }

        void BirhtBookData_ReadFail(object sender, EventArgs e)
        {
            //    throw new NotImplementedException();
        }

        void BirhtBookData_ExportSuccess(object sender, EventArgs e)
        {
            //    throw new NotImplementedException();
        }

        void BirhtBookData_ExportFail(object sender, EventArgs e)
        {
            //    throw new NotImplementedException();
        }

        private void Notify(string p)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(p));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void bback_Click(object sender, RoutedEventArgs e)
        {
            parentgrid.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void bApplyFilter_Click(object sender, RoutedEventArgs e)
        {
            FilterSearch();
            FilterWindow.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void FilterSearch()
        {
            CollectionViewSource.GetDefaultView(lvBirth.ItemsSource).Refresh();
        }

        private bool _filter(object item)
        {
            try
            {
                BirthBookRecord b = item as BirthBookRecord;

                if (isnull(cfirst) && isnull(csur) && isnull(momfirst) && isnull(momsur) && isnull(dadfirst) && isnull(dadsur) && isnull(Ort))
                    return true;

                bool rv = true;

                if (!isnull(cfirst))
                {
                    rv = (b.FirstName.IndexOf(cfirst.Text, StringComparison.OrdinalIgnoreCase) >= 0);
                }

                if (!isnull(csur))
                {
                    rv &= (b.SurName.IndexOf(csur.Text, StringComparison.OrdinalIgnoreCase) >= 0);
                }

                if (!isnull(momfirst))
                {
                    rv &= (b.MotherFirstName.IndexOf(momfirst.Text, StringComparison.OrdinalIgnoreCase) >= 0);
                }
                if (!isnull(momsur))
                {
                    rv &= (b.MotherSurName.IndexOf(momsur.Text, StringComparison.OrdinalIgnoreCase) >= 0);
                }

                if (!isnull(dadfirst))
                {
                    rv &= (b.FatherFirstName.IndexOf(dadfirst.Text, StringComparison.OrdinalIgnoreCase) >= 0);
                }

                if (!isnull(dadsur))
                {
                    rv &= (b.FatherSurName.IndexOf(dadsur.Text, StringComparison.OrdinalIgnoreCase) >= 0);
                }

                if (!isnull(Ort))
                {
                    rv &= (b.Address.IndexOf(Ort.Text, StringComparison.OrdinalIgnoreCase) >= 0);
                }

                return rv;
            }
            catch
            { }
            return false;
        }

        private bool isnull(TextBox t)
        {
            return string.IsNullOrEmpty(t.Text);
        }

        private void bFilter_Click(object sender, RoutedEventArgs e)
        {
            FilterWindow.Visibility = System.Windows.Visibility.Visible;
        }

        private void lvBirth_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lvBirth.SelectedItems.Count == 1)
            {
                CurItem = lvBirth.SelectedItem as BirthBookRecord;
            }
        }
    }
}
